#!/usr/bin/env bash

function _sync_installation_scripts() {
	echo "-----------------------------------------------------------------------------"
	echo -e " Checking for changes of the installation scripts in the online database..."
	rm -R -f "$AMPATH"/.cache/sync-args && echo "" >> "$AMPATH"/.cache/sync-args
	cd "$APPSPATH" && find -name 'AM-updater' -printf "%h\n" 2>/dev/null | sort -u | cut -c 3- > "$AMPATH"/.cache/sync-args
	ARGS=$(cat "$AMPATH"/.cache/sync-args)

	for arg in $ARGS; do
		mkdir -p "$APPSPATH/$arg/.am-installer"
		if test -f "$APPSPATH/$arg/.am-installer/$arg"; then
			CURRENT=$(cat "$APPSPATH"/$arg/.am-installer/$arg)
			SOURCE=$(wget -q "$APPSDB"/$arg -O -)
			if [ "$CURRENT" == "$SOURCE" ]; then
				echo -ne "\r" 2> /dev/null
			else
				echo -e " ◆ Changed https://github.com/ivan-hc/AM/blob/main/programs/$arch/$arg"
			fi
		else
			if curl --output /dev/null --silent --head --fail "$APPSDB"/$arg  1>/dev/null; then
				echo -e " ◆ No installation script for $arg, downloading one..."
				mkdir -p "$APPSPATH/$arg/.am-installer"
				wget -q "$APPSDB/$arg" -O "$APPSPATH/$arg/.am-installer/$arg"
			fi
		fi
	done
}

function _sync_modules() {
	echo "-----------------------------------------------------------------------------"
	echo -e " Check for updates in modules..."
	MODULES=$(curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/ivan-hc/AM/contents/modules?ref="$AMBRANCH" | sed 's/[()",{}]/ /g; s/ /\n/g' | grep -o 'https.*raw.*modules.*am$')
	for v in $MODULES; do
		cd "$AMPATH"/modules || return
		MODULENAME=$(echo "$v" | sed 's:.*/::')
		if ! test -f ./$MODULENAME; then
			echo " ◆ Downloading $MODULENAME (not previously installed)..."
			wget -q "$v"
			chmod a+x ./$MODULENAME
		fi
		CURRENT=$(cat ./$MODULENAME)
		SOURCE=$(wget -q "$v" -O -)
		if [ "$CURRENT" == "$SOURCE" ]; then
			echo -ne "\r" 2> /dev/null
		else
			echo " ◆ Updating $MODULENAME..."
			mkdir tmp
			cd tmp || return
			wget -q "$v"
			cd ..
			mv tmp/*.am ./
			chmod a+x ./*.am
			rmdir tmp
		fi
	done
}

function _sync_amcli() {
	echo "-----------------------------------------------------------------------------"
	_completion_lists
	rm -f /opt/am/appman
	if [ "$AMCLIPATH" == /opt/am/appman ] 2>/dev/null; then
		AMCLIPATH="/opt/am/APP-MANAGER"
		CURRENT_AM_VERSION=$("$AMCLIPATH" -v)
	else
		CURRENT_AM_VERSION=$("$AMCLIPATH" -v)
	fi
	echo -ne '\n ◆ SYNCHRONIZING "'"$(echo "$AMCLI" | tr a-z A-Z)"'" VERSION '"$CURRENT_AM_VERSION"'...\r'; sleep 1
	rm -R -f "$AMPATH"/.cache/* 1>/dev/null; cd "$AMPATH"/.cache || return
	if [ "$AMCLI" == am ] 2>/dev/null; then
		wget -q "$AMREPO"/APP-MANAGER && chmod a+x ./APP-MANAGER && chmod 777 ./APP-MANAGER
		cd ..
		echo y | mv "$AMPATH"/.cache/APP-MANAGER /opt/am/APP-MANAGER
	else
		wget -q "$AMREPO"/APP-MANAGER -O appman && chmod a+x ./"$AMCLI"
		cd ..
		echo y | mv "$AMPATH"/.cache/"$AMCLI" "$AMCLIPATH"
	fi
	if [ ! "$CURRENT_AM_VERSION" == "$("$AMCLIPATH" -v)" ] ; then
		echo -ne ' A new release of "'"$(echo "$AMCLI" | tr a-z A-Z)"'" is available, please wait...\r'
		echo -e ' ◆ "'"$(echo "$AMCLI" | tr a-z A-Z)"'" IS NOW UPDATED TO THE BRAND NEW '"$("$AMCLIPATH" -v)"' VERSION!           \n\n  Replacement of version '"$CURRENT_AM_VERSION"' currently in use, COMPLETED!'
		echo -e "\n See https://github.com/ivan-hc/AM/commits/main\n"
	else
		echo -e ' ◆ "'"$(echo "$AMCLI" | tr a-z A-Z)"'" IS ALREADY UPDATED, CURRENT VERSION '"$CURRENT_AM_VERSION"''
		echo -e "\n See https://github.com/ivan-hc/AM/commits/$AMBRANCH\n"
	fi
}

if [[ "$AMREPO" = *://* ]]; then
	_sync_installation_scripts
	_sync_modules
	_sync_amcli
fi
